package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingQuery;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.service.SetmealService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    @Reference
    SetmealService setmealService;

    @Reference
    OrderService orderService;



    @RequestMapping("/setNumberByDate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.setNumberByDate(orderSetting);
        return Result.success("");
    }

    @RequestMapping("/phoneSetOrderSetting")
    public Result phoneSetOrderSetting(@RequestBody Map map){
        String phoneNumber = (String) map.get("phoneNumber");
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");
        String name = (String) map.get("name");
        Result result=null;

        try {
             result=orderService.phoneSetOrderSetting(map);
            if (result.isFlag()) {
                System.out.println("发送预约成功短信");
            }
            return Result.success("预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("错误");
        }




    }

    @RequestMapping("/findOrder")
    public Result findOrder(){
        try {
            JSONArray setmeals=setmealService.getSetmeal();
            return Result.success("",setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("??");
        }
    }



    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //使用POI工具读取
            List<String[]> datas = POIUtils.readExcel(excelFile); //list每一个数组代表excel一行数据

            //把datas 转成List<OrderSetting>
            if(CollectionUtil.isNotEmpty(datas)){
                List<OrderSetting> params = new ArrayList<>();
                for (String[] row : datas) {
                    if(row.length != 2){
                        continue;
                    }
                    String dateStr = row[0];// 日期
                    String numberStr = row[1];//数量
                    if(StrUtil.isBlank(dateStr) || StrUtil.isBlank(numberStr)){
                       continue;
                    }
                    OrderSetting orderSetting = new OrderSetting(DateUtil.parse(dateStr, "yyyy/MM/dd"), Integer.valueOf(numberStr));
                    params.add(orderSetting);
                }
                //保存到数据库
                orderSettingService.batchSave(params);
            }
            return Result.success("");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(@RequestBody OrderSettingQuery query){
       //{"date":22,"mouth":0,"number":300,"reservations":300,}
        List<OrderSetting> orderSettings = orderSettingService.findByMonth(query.getMm());
//        List<Map> results = new ArrayList<>();
//        for (OrderSetting orderSetting : orderSettings) {
//            Map map = new HashMap();
//            map.put("number",orderSetting.getNumber());
//            map.put("reservations",orderSetting.getReservations());
//            map.put("date",orderSetting.getOrderDate().getDate());
//            map.put("mouth",orderSetting.getOrderDate().getMonth());
//            results.add(map);
//        }
        return Result.success("",orderSettings);

    }

}
